﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HPortal.Lib;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

namespace HPortal.Module
{
    public partial class LeftMenu : PanelControl
    {
        private DataTable channel;
        private int ChannelID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    bool isEN = (bool)System.Web.HttpContext.Current.Session["isEN"];
                    if (isEN)
                    {
                        ChannelID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["rootChannelIDEN"].ToString());
                        channel = getChannel(1);
                    }
                    else
                    {
                        ChannelID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["rootChannelIDVN"].ToString());
                        channel = getChannel(0);
                    }
                }
                catch (Exception ex)
                {
                    ChannelID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["rootChannelIDVN"].ToString());
                    channel = getChannel(0);
                }
                LoadMenu(mnuChannel.Items, 0);
            }
        }
        private DataTable getChannel(int language)
        {
            string where = string.Empty;
            if (this.Context.User.Identity.IsAuthenticated)
            {
                string[] arrRoleName = Roles.GetRolesForUser(this.Context.User.Identity.Name);
                foreach (string roleName in arrRoleName)
                {
                    where += DatabaseHelper.EscapeQuoteUnicode(roleName) + ",";
                }

            }
            where += "'Anonymous'";
            where = "WHERE ChannelID IN (SELECT DISTINCT ChannelID FROM [ChannelRole] WHERE ([Role] IN (" + where + ") AND (Viewed=1 OR Edited=1 OR Approved=1 OR Administrated=1) AND Language=" + language +" AND Locked=0))";
            string query = "SELECT * FROM [Channel] " + where + " ORDER BY Language,[ParentID],[Priority],[ChannelID]";
            return DatabaseHelper.Select(query);
        }

        public void LoadMenu(MenuItemCollection mic, int ParentID)
        {
            int j = 0;
            if (channel != null)
            {
                while (j < channel.Rows.Count)
                {
                    DataRow dr = channel.Rows[j];
                    string name = dr["Name"].ToString();
                    string channelID = dr["ChannelID"].ToString();
                    string language = dr["Language"].ToString();
                    string link = "/HPortal/Default.aspx?ChannelID=" + channelID;
                    string parentID = dr["ParentID"].ToString();
                    if (int.Parse(parentID) == ParentID)
                    {
                        channel.Rows.Remove(dr);
                        MenuItem mi = new MenuItem(name, channelID, null, link, "");
                        mi.Selectable = !hasChildMenu(channelID);
                        mic.Add(mi);
                        this.LoadMenu(mi.ChildItems, int.Parse(channelID));
                        continue;
                    }
                    j++;
                }
            }
        }
        private bool hasChildMenu(string channelID)
        {
            int rCount = channel.Rows.Count;
            for (int i = 0; i < rCount; i++)
                if (channel.Rows[i]["ParentID"].ToString() == channelID)
                    return true;
            return false;
        }
    }
}